import BaseRedTipManager from "./BaseRedTipManager";
import RedTipInfo from "./RedTipInfo";
import TSMap from "../math/TSMap";
import { RedTipPos } from "./RedTipConst";
import RedTipAble from "../interface/RedTipAble";
// import BaseView from "../base/view/BaseView";

export default class WidgetTipManager extends BaseRedTipManager {

    private widgetMap: TSMap = new TSMap();



    create(layerName, widgetName, redType) {
        if (!this.widgetMap.has(layerName)) {
            this.widgetMap.set(layerName, new TSMap());
        }

        if (!this.widgetMap.get(layerName).has(widgetName)) {
            this.widgetMap.get(layerName).set(widgetName, new TSMap());
        }

        if (!this.widgetMap.get(layerName).get(widgetName).has(redType)) {
            this.widgetMap.get(layerName).get(widgetName).set(redType, new RedTipInfo(redType));
        }
    }

    /**
     * 
     * @param layerName 
     * @param widgetName 
     * @param redType 
     */
    add(layerName, widgetName, redType, id) {
        this.create(layerName, widgetName, redType);

        let tipInfo: RedTipInfo = this.widgetMap.get(layerName).get(widgetName).get(redType);
        if (tipInfo) {
            // cc.log(" add count 11111111111111111 ",tipInfo.getCount()); 
            let flag = tipInfo.add(id);
            // cc.log(" add count 22222222222222222 ",tipInfo.getCount()); 
            if (tipInfo.getCount() == 1) {
                return true
            }
            // return flag;        
        } else {
            cc.log("tipInfo is null ");
        }

        return false;
    }

    print() {
        // cc.log(" value ",this.widgetMap);
    }

    /**
     * 
     * @param layerName 
     * @param widgetName 
     */
    getWidgetInfo(layerName, widgetName, redType?) {
        let self = this;
        if (!self.widgetMap.has(layerName)) {
            return null;
        }
        if (!self.widgetMap.get(layerName).has(widgetName)) {
            return null;
        }
        if (redType) {
            if (self.widgetMap.get(layerName).get(widgetName).has(redType)) {
                return self.widgetMap.get(layerName).get(widgetName).get(redType);
            } else {
                return null;
            }
        } else {
            return self.widgetMap.get(layerName).get(widgetName);
        }

    }

    /**
     * 
     * @param layerName 
     * @param widgetName 
     * @param redType 
     */
    delete(layerName, widgetName, redType, id) {
        // cc.log("delete  layerName ",layerName," widgetName ",widgetName," redType",redType);
        if (!this.widgetMap.has(layerName)) {
            return false;
        }
        if (!this.widgetMap.get(layerName).has(widgetName)) {
            return false;
        }
        if (!this.widgetMap.get(layerName).get(widgetName).has(redType)) {
            return false;
        }
        let tipInfo: RedTipInfo = this.widgetMap.get(layerName).get(widgetName).get(redType);
        // cc.log(" delete count 11111111111111111 ",tipInfo.getCount());     
        let flag = tipInfo.delete(id);
        // cc.log(" delete count 2222222222222222222 ",tipInfo.getCount());     
        if (tipInfo.getCount() <= 0) {
            this.widgetMap.get(layerName).get(widgetName).delete(redType);
            return true;
        } else {

        }

        return false;
    }



    /**
     * 
     * @param layer 
     * @param widgetVector 
     */
    deleteRedTip(layer: RedTipAble, widgetVector: Array<any>, redType?, id?) {
        let layerName = layer.getRedTipName();
        // cc.log("deleteRedTip layerName "+layerName);
        if (widgetVector) {
            for (let index = 0; index < widgetVector.length; index++) {
                const data = widgetVector[index];
                let widgetName = data[RedTipPos.widgetPath]
                if (!widgetName) {
                    widgetName = layerName;
                }
                let flag = false;
                if (!redType) {
                    // cc.log("deleteRedTip 111 widgetName "+widgetName);
                    let widgetMap: TSMap = this.getWidgetInfo(layerName, widgetName);
                    // cc.log(" widgetInfo.getCount() ",widgetInfo.size());
                    if (widgetMap && widgetMap.size() <= 0) {
                        flag = true;
                    }
                } else {
                    // cc.log("deleteRedTip  222 widgetName "+widgetName);
                    let widgetInfo: RedTipInfo = this.getWidgetInfo(layerName, widgetName, redType);
                    // cc.log(" widgetInfo.getCount() ",widgetInfo.size());
                    if (!widgetInfo || !widgetInfo.has(id)) {
                        flag = true;
                    }
                }
                // cc.log('  deleteRedTip   flag ',flag);
                if (flag) {
                    let widget = null;
                    if (widgetName == "this") {
                        widget = layer.getRedTipNode();
                    } else {
                        widget = cc.find(widgetName, layer.getRedTipNode());
                    }
                    if (widget) {
                        this.deleteRedSprite(widget);
                    }
                    // let widget = cc.find(widgetName,layer.node);
                    // if(widget){
                    //     this.deleteRedSprite(widget);
                    // }
                }
            }
        }
    }

    /**
     * 
     * @param layer 
     * @param widgetVector 
     */
    addRedTip(layer: RedTipAble, widgetVector: Array<any>, redType?, id?) {
        let layerName = layer.getRedTipName()
        // cc.log("addRedTip layerName "+layerName);
        for (let index = 0; index < widgetVector.length; index++) {
            const data = widgetVector[index];
            let widgetName = data[RedTipPos.widgetPath]
            if (!widgetName) {
                widgetName = layerName;
            }
            let flag = false;
            if (!redType) {
                // cc.log("addRedTip 1111 widgetName " + widgetName);
                let widgetMap: TSMap = this.getWidgetInfo(layerName, widgetName);
                // cc.log(" widgetInfo.getCount() ",widgetMap.size());
                if (widgetMap && widgetMap.size() > 0) {
                    flag = true;
                }
            } else {
                // cc.log("addRedTip 22222 widgetName " + widgetName);
                let widgetInfo: RedTipInfo = this.getWidgetInfo(layerName, widgetName, redType);
                // cc.log(" widgetInfo.getCount() ",widgetInfo.size());
                if (widgetInfo && widgetInfo.has(id)) {
                    flag = true;
                }
            }
            // cc.log(' add red tip ', flag)
            if (flag) {
                let widget = null;
                if (widgetName == "this") {
                    widget = layer.getRedTipNode();
                } else {
                    widget = cc.find(widgetName, layer.getRedTipNode());
                }
                if (widget) {
                    this.addRedSprite(widget, data);
                } else {
                    cc.warn(' error  widget not find ')
                }
            }

        }
    }


    deleteOtherRedTip(layer: RedTipAble, widgetVector, redType, id) {
        let layerName = layer.getRedTipName()
        for (let index = 0; index < widgetVector.length; index++) {
            const data = widgetVector[index];
            let widgetName = data[RedTipPos.widgetPath]
            if (!widgetName) {
                widgetName = layerName;
            }
            let widgetInfo: RedTipInfo = this.getWidgetInfo(layerName, widgetName, redType);
            if (widgetInfo && widgetInfo.getCount() <= 0) {
                let widget = cc.find(widgetName, layer.getRedTipNode());
                if (widget) {
                    this.deleteRedSprite(widget);
                }
            }
        }
    }
}
